// 引入http模块
const http = require('http')

// 静态的网站url
// let url = 'http://www.itcast.cn/newsvideo/newslist.html'

// 动态网站的ajax请求
let url = 'http://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json'

// 创建请求对象
let reqOption = {
  method: 'get',
  headers: {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Cookie": "UM_distinctid=170578613fc2e8-01b7f8702ed6e7-39647b0e-13c680-170578613fd102; qimo_seosource_b2f10070-624e-11e8-917f-9fb8db4dc43c=%E7%AB%99%E5%86%85; qimo_seokeywords_b2f10070-624e-11e8-917f-9fb8db4dc43c=; href=http%3A%2F%2Fwww.itcast.cn%2Fnewsvideo%2Fnewslist.html; accessId=b2f10070-624e-11e8-917f-9fb8db4dc43c; pageViewNum=1; Hm_lvt_0cb375a2e834821b74efffa6c71ee607=1582016371; Hm_lpvt_0cb375a2e834821b74efffa6c71ee607=1582016371; bad_idb2f10070-624e-11e8-917f-9fb8db4dc43c=f9956e61-522c- 11ea-8962-cf63da69c47c; nice_idb2f10070-624e-11e8-917f-9fb8db4dc43c=f9956e62-522c-11ea-8962-cf63da69c47c; openChatb2f10070-624e-11e8-917f-9fb8db4dc43c=true; parent_qimo_sid_b2f10070-624e-11e8-917f-9fb8db4dc43c=fde05e80-522c-11ea-b362-d76f856af26c; CNZZDATA4617777=cnzz_eid%3D1038113763-1582010975-%26ntime%3D1582016383",
    "Host": "www.itcast.cn",
    "Pragma": "no-cache",
    "Referer": "http://www.itcast.cn/newsvideo/newslist.html",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36",
    "X-Requested-With": "XMLHttpRequest"
  }
}

let req = http.request(url, reqOption, res => {
  // 一段一段的
  let chunks = []
  res.on('data', chunk => chunks.push(chunk))

  res.on('end', () => {
    let result = Buffer.concat(chunks).toString('utf-8')
    console.log(JSON.parse(result))



  })
})

// 发送请求
req.end()
